﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScoreManagement
{
    public partial class Login : Form
    {
        private string url = "server=127.0.0.1; port=3306;database=exercise;uid=root;pwd=123456";
        public Login()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.reset();
        }
        public void reset()
        {
            // 重置按钮：清空输入框里面的值
            this.textBox1.Text = "";
            this.textBox2.Text = "";
            this.radioButton1.Checked = true;
            this.radioButton2.Checked = false;
            this.radioButton3.Checked = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string password = null;
            // 判断是不是学生登录
            if (radioButton1.Checked == true)
            {
                // string.Format格式化字符串
                string sql = string.Format(@"SELECT password FROM s_login WHERE username='{0}'", textBox1.Text);
                // 执行sql    返回一个结果集
                MySqlConnection conn = new MySqlConnection(url);
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                conn.Open();
                MySqlDataReader dr = cmd.ExecuteReader();
                int count = 0;
                // 一行一行 往下读
                while (dr.Read())
                {
                    password = dr["password"].ToString();
                    count++;
                }
                string username = this.textBox1.Text;
                if (count > 0 && password.Equals(textBox2.Text))
                {
                    MessageBox.Show("登录成功");
                    // 创建 学生菜单的实例
                    Form1 stu = new Form1(this, username, radioButton1.Text);
                    // 这个是展示窗口
                    stu.Show();
                    // 展示窗体之后，把前一个打开的窗体关闭
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("账号密码错误");
                }
            }
            else if (radioButton2.Checked == true)
            {
                string sql = string.Format(@"SELECT password FROM t_login WHERE username='{0}'", textBox1.Text);
                // 执行sql    返回一个结果集
                MySqlConnection conn = new MySqlConnection(url);
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                conn.Open();
                MySqlDataReader dr = cmd.ExecuteReader();
                int count = 0;
                while (dr.Read())
                {
                    password = dr["password"].ToString();
                }
                string username = this.textBox1.Text;
                if (password.Equals(textBox2.Text))
                {
                    MessageBox.Show("登录成功");
                    Form1 teacher = new Form1(this, username, radioButton2.Text);
                    teacher.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("账号密码失败");
                }
            }
            else {
                string sql = string.Format(@"SELECT password FROM a_login WHERE username='{0}'", textBox1.Text);
                // 执行sql    返回一个结果集
                MySqlConnection conn = new MySqlConnection(url);
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                conn.Open();
                MySqlDataReader dr = cmd.ExecuteReader();
                int count = 0;
                while (dr.Read())
                {
                    password = dr["password"].ToString();
                }
                string username = this.textBox1.Text;
                if (password.Equals(textBox2.Text))
                {
                    MessageBox.Show("登录成功");
                    Form1 teacher = new Form1(this, username, radioButton3.Text);
                    teacher.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("账号密码失败");
                }
            }
        }
    }
}
